package com.ipay.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.ipay.exception.BaseException;
import com.ipay.utils.Encoder;
import com.ipay.utils.HttpClientUtils;

@Component
public class SMSService {
	
	private String url = "http://www.api.zthysms.com/";
	/**
	 * 账号：hnrw888hy 密码：7Ukrwh
	 * 账号：hnrw888yx 密码：1tEsMt  
	 * 
	 */
	private String user = "hnrw888hy";
	private String password = "7Ukrwh";
	
	static Map<String, String> error = new HashMap<String, String>();
	static {
		error.put("-1", "发送失败");
		error.put("11", "产品错误（联系客服）");
		error.put("12", "产品禁用（联系客服）");
		error.put("13", "手机号码错误，不支持的号段");
		error.put("15", "签名不合规");
		error.put("16", "签名屏蔽");
		error.put("17", "代表签名分配扩展失败");
		error.put("18", "短信内容不能为空");
		error.put("19", "短信内容最大1000个字");
		error.put("20", "预付费用户条数不足");
		error.put("21", "发送内容存在黑词");
		error.put("22", "通道错误（联系客服）");
		error.put("28", "签名最长15个字");
		error.put("29", "小号错误");
		error.put("98", "异常（联系客服）");
		error.put("99", "DES解密Exception");
	}
	
	public void sendMessage(String mobile, String text) {
		Map<String, String> param = new HashMap<String,String>();
		String tkey = this.getTkey();
		param.put("username", user);
		param.put("tkey", tkey);
		param.put("password", this.getPassword(tkey));
		param.put("mobile", mobile);
		param.put("content", text);
		try {
			String ret = HttpClientUtils.getResponseText(url + "sendSms.do", param, new HashMap<String,String>());
			System.out.println("ret=="+ret);
			if(ret != null && ret.length() > 0) {
				String s = ret.substring(0,ret.indexOf(','));
				if("1".equals(s)){ return;}
				String errorInfo = error.get(s);
				if(errorInfo == null) {errorInfo = "未知异常：" + ret;}
				throw new BaseException("999999",errorInfo);
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new BaseException("999999","发送短信异常");
		}
	}
	
	
	private String getPassword(String key) {
		try {
			byte[] passwod = this.password.getBytes("utf-8");
			byte[] b = MessageDigest.getInstance("MD5").digest(passwod);
			String pwd =  Encoder.getInstance("hex").encode(b);
			byte[] b1 = MessageDigest.getInstance("MD5").digest((pwd + key).getBytes("utf-8"));
			return Encoder.getInstance("hex").encode(b1);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private String getTkey() {
		Date d = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
		return fmt.format(d);
	}
	
	public static void main(String[] args) {
		Date d = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
		System.out.println(fmt.format(d));
		SMSService sms=new SMSService();
		sms.sendMessage("15627285187","【道合聚盈】您的验证码是201377。如非本人操作，请忽略本短信");
	}
	
	
}
